package com.yidiandao.business.ui.widget.listview.displaylistitem;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.imagemodule.FlashSale;
import com.yidiandao.business.ui.goodsdetail.GoodsDetailActivity;
import com.yidiandao.business.ui.url.UrlActivity;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.viewholder.HomeFlashSaleHolder;
import com.yidiandao.business.ui.widget.view.TimerLayout;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/**
 * 首页 限时抢购 Item ListItem
 */
public class HomeFlashSaleListItem extends DisplayListItem<HomeFlashSaleHolder, FlashSale>
        implements View.OnClickListener, TimerLayout.OnTimeChangeListener {

    private Activity mActivity;

    private HomeFlashSaleHolder mHolder;

    private Goods mFlashGoods1;
    private Goods mFlashGoods2;
    private Goods mFlashGoods3;

    private String mMoreUrl;

    public HomeFlashSaleListItem(FlashSale s) {
        super(s);
    }

    @Override
    public void onShow(Context context, HomeFlashSaleHolder holder) {
        mActivity = (Activity) context;
        mHolder = holder;

        mMoreUrl = data.getUrl();
        List<Goods> flashGoodsList = data.getGoodsList();

        try {
            // 第一个
            mFlashGoods1 = flashGoodsList.get(0);
            holder.refreshUI1(mFlashGoods1);
            refreshUIbyState(mFlashGoods1, holder.timerLayout1, holder.rootState1, holder.tvState1, holder.toStartTimerLayout1);

            // 事件
            holder.ratioLayout1.setOnClickListener(this);
            holder.timerLayout1.setOnTimeChangeListener(this);
            holder.tvNewPrice1.setOnClickListener(this);
            holder.toStartTimerLayout1.setOnTimeChangeListener(this);

            // 第二个
            mFlashGoods2 = flashGoodsList.get(1);
            holder.limitLayout2.setVisibility(View.VISIBLE);
            holder.refreshUI2(mFlashGoods2);
            refreshUIbyState(mFlashGoods2, holder.timerLayout2, holder.rootState2, holder.tvState2, holder.toStartTimerLayout2);

            // 事件
            holder.ratioLayout2.setOnClickListener(this);
            holder.timerLayout2.setOnTimeChangeListener(this);
            holder.tvNewPrice2.setOnClickListener(this);
            holder.toStartTimerLayout2.setOnTimeChangeListener(this);

            // 第三个
            mFlashGoods3 = flashGoodsList.get(2);
            holder.limitLayout3.setVisibility(View.VISIBLE);
            holder.refreshUI3(mFlashGoods3);
            refreshUIbyState(mFlashGoods3, holder.timerLayout3, holder.rootState3, holder.tvState3, holder.toStartTimerLayout3);

            // 事件
            holder.ratioLayout3.setOnClickListener(this);
            holder.timerLayout3.setOnTimeChangeListener(this);
            holder.tvNewPrice3.setOnClickListener(this);
            holder.toStartTimerLayout3.setOnTimeChangeListener(this);

        } catch (IndexOutOfBoundsException e) {
            if (mFlashGoods2 == null) {
                holder.limitLayout2.setVisibility(View.INVISIBLE);
            }

            if (mFlashGoods3 == null) {
                holder.limitLayout3.setVisibility(View.INVISIBLE);
            }
        }

        holder.tvMore.setOnClickListener(this);
    }

    @Override
    public String getDisplayItemTag() {
        return HomeFlashSaleListItem.class.getSimpleName();
    }

    @Override
    public Class<HomeFlashSaleHolder> getViewHolderClazz() {
        return HomeFlashSaleHolder.class;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_enter:
                UrlActivity.actionStart(mActivity, mMoreUrl);
                break;
            case R.id.ratio_layout_1:
                toGoodsDetailActivity(mFlashGoods1);
                break;
            case R.id.ratio_layout_2:
                toGoodsDetailActivity(mFlashGoods2);
                break;
            case R.id.ratio_layout_3:
                toGoodsDetailActivity(mFlashGoods3);
                break;
//            case R.id.tv_new_price_1:
//            case R.id.tv_new_price_2:
//            case R.id.tv_new_price_3:
//                if (!PriceUtils.isLogin()) {
//                    LoginActivity.actionStart(mActivity);
//                }
//                break;
        }
    }

    /**
     * 根据当前 限时抢购商品 的状态，来确定显示那种UI
     *
     * @param flashGoods         当前商品
     * @param timerLayout        顶部倒计时(活动距离结束的倒计时)
     * @param stateLayout        活动商品的状态Layout(未开始/已结束/已售罄)
     * @param tvState            活动商品的状态TextView(距离开始xxx/已结束/已售罄)
     * @param toStartTimerLayout 距离活动开始的倒计时
     */
    private void refreshUIbyState(Goods flashGoods, TimerLayout timerLayout, ViewGroup stateLayout,
                                  TextView tvState, TimerLayout toStartTimerLayout) {

        // 当前的状态
        int state = flashGoods.getState();

        switch (state) {
            // 未开始
            case Goods.ACTIVE_STATE.UN_START:
                stateLayout.setVisibility(View.VISIBLE);
                tvState.setText(R.string.to_start);
                timerLayout.unTimer();

                // 设置距离开始
                toStartTimerLayout.setVisibility(View.VISIBLE);
                toStartTimerLayout.setLeaveTime(flashGoods.getToStartTime());
                break;

            // 进行中
            case Goods.ACTIVE_STATE.RUNNING:
                // 需要判定是否是否抢购完毕
                if (flashGoods.getIsSaleOff() == Goods.ACTIVE_STATE.SALE_OFF) {
                    // 如果抢购完毕 显示 蒙版-->已售罄
                    stateLayout.setVisibility(View.VISIBLE);
                    tvState.setText(R.string.sale_off);
                } else {
                    // 未抢购完毕
                    stateLayout.setVisibility(View.GONE);
                }
                timerLayout.setLeaveTime(flashGoods.getToEndTime());
                break;

            // 已结束
            case Goods.ACTIVE_STATE.FINISHED:
                stateLayout.setVisibility(View.VISIBLE);
                timerLayout.setLeaveTime(0);
                tvState.setText(R.string.over);

                toStartTimerLayout.setVisibility(View.GONE);
                break;
        }
    }

    private void toGoodsDetailActivity(Goods flashGoods) {
        GoodsDetailActivity.actionStart(mActivity, flashGoods.getGoodsId());
    }

    @Override
    public void onLeaveTimeChanged(int viewId, long leaveTime) {
        switch (viewId) {

            // 距离活动结束倒计时
            case R.id.timer_layout_1:
                refreshStateUIPerSeconds(leaveTime, mHolder.rootState1, mHolder.tvState1, mFlashGoods1);
                break;

            case R.id.timer_layout_2:
                refreshStateUIPerSeconds(leaveTime, mHolder.rootState2, mHolder.tvState2, mFlashGoods2);
                break;

            case R.id.timer_layout_3:
                refreshStateUIPerSeconds(leaveTime, mHolder.rootState3, mHolder.tvState3, mFlashGoods3);
                break;

            // ---------------------------------------------------------------------------

            // 从距离开始 ----> 到 ---> 开始抢购
            case R.id.to_start_timer_layout_1:
                refreshToStartUIPerSeconds(leaveTime, mHolder.rootState1, mHolder.tvState1,
                        mHolder.timerLayout1, mHolder.toStartTimerLayout1, mFlashGoods1);
                break;
            case R.id.to_start_timer_layout_2:
                refreshToStartUIPerSeconds(leaveTime, mHolder.rootState2, mHolder.tvState2,
                        mHolder.timerLayout2, mHolder.toStartTimerLayout2, mFlashGoods2);
                break;
            case R.id.to_start_timer_layout_3:
                refreshToStartUIPerSeconds(leaveTime, mHolder.rootState3, mHolder.tvState3,
                        mHolder.timerLayout3, mHolder.toStartTimerLayout3, mFlashGoods3);
                break;
        }
    }

    /**
     * 每隔1秒刷新UI
     *
     * @param leaveTime  倒计时剩余时间
     * @param rootState  状态Layout
     * @param tvState    状态TextView
     * @param flashGoods 当前的活动商品
     */
    private void refreshStateUIPerSeconds(
            long leaveTime, RelativeLayout rootState, TextView tvState, Goods flashGoods) {
        // 判断状态 活动是否已经结束
        if (leaveTime == 0) {
            // 活动已结束
            rootState.setVisibility(View.VISIBLE);
            tvState.setText(R.string.over);
        } else {
            // 活动未结束
            if (flashGoods.getIsSaleOff() == Goods.ACTIVE_STATE.SALE_OFF) {
                // 如果抢购完毕 显示 蒙版-->已售罄
                rootState.setVisibility(View.VISIBLE);
                tvState.setText(R.string.sale_off);
            } else {
                rootState.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 刷新距离开始的状态Ui
     *
     * @param leaveTime          剩余多少时间开始
     * @param rootState          状态Layout
     * @param tvState            状态TextView
     * @param timerLayout        活动倒计时Layout
     * @param toStartTimerLayout 距离开始倒计时
     * @param flashGoods         当前活动商品
     */
    private void refreshToStartUIPerSeconds(
            long leaveTime,
            RelativeLayout rootState,
            TextView tvState,
            TimerLayout timerLayout,
            TimerLayout toStartTimerLayout,
            Goods flashGoods) {
        // 未开始状态 到 开始状态
        if (leaveTime == 0) {
            rootState.setVisibility(View.GONE);
            flashGoods.setState(Goods.ACTIVE_STATE.RUNNING);
            refreshUIbyState(flashGoods, timerLayout, rootState, tvState, toStartTimerLayout);
        } else {
            tvState.setText(UIUtils.getString(R.string.to_start_leave_time, toStartTimerLayout.getTimeString()));
        }
    }
}
